package edu.scau.hibernate.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import edu.scau.hibernate.model.User;
import edu.scau.hibernate.util.HibernateUtil;

public class BaseDaoImpl<T> implements BaseDao<T> {

	private Session session;
	public BaseDaoImpl(){
		session=HibernateUtil.getSession();
	}

	@Override
	public void save(T t) throws Exception {
		session.save(t);
	}

	@Override
	public void delete(T t) throws Exception {
		session.delete(t);
	}

	@Override
	public void update(T t) throws Exception {
		session.update(t);
	}

	@Override
	public T findById(int ID) throws Exception {
		T t = null;
		String hql="from User user where ID='"+ID+"'";
		Query query=session.createQuery(hql);
		List list=query.list();
		if(list.size()==1)
		    t=(T) list.get(0);
		return t;
	}
	@Override
	public List<T> findAll() throws Exception {
		String hql="from User user";
		Query query=session.createQuery(hql);
		List list=query.list();
		return list;
	}


}
